import AMapLoader from '@amap/amap-jsapi-loader';
//地图
//路径实例
//巡航器

let mapLoader = AMapLoader.load({
  "key": "3eb901b56f70f154055eac3b58c26858",              // 申请好的Web端开发者Key，首次调用 load 时必填
  "version": "1.4.15",   // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
  "plugins": [],           // 需要使用的的插件列表，如比例尺'AMap.Scale'等
  "AMapUI": {             // 是否加载 AMapUI，缺省不加载
    "version": '1.1',   // AMapUI 缺省 1.1
    "plugins":['misc/PathSimplifier','overlay/SimpleMarker','overlay/SimpleInfoWindow'],       // 需要加载的 AMapUI ui插件
  },
  "Loca":{                // 是否加载 Loca， 缺省不加载
    "version": '1.3.2'  // Loca 版本，缺省 1.3.2
  },
});

//初始化简单路径


export function initPath(map){
  return initMap(map).then((map) =>{
    let pathSimplifierIns = new AMapUI.PathSimplifier({
      zIndex: 100,
      //autoSetFitView:false,
      map: map, //所属的地图实例

      getPath: function(pathData, pathIndex) {
        return pathData.path;
      },
      getHoverTitle: function(pathData, pathIndex, pointIndex) {

        if (pointIndex >= 0) {
          //point
          return pathData.name + '，点：' + pointIndex + '/' + pathData.path.length;
        }

        return pathData.name + '，点数量' + pathData.path.length;
      },
      renderOptions: {

        renderAllPointsIfNumberBelow: 100 //绘制路线节点，如不需要可设置为-1
      },
    });
    return new Promise(resolve => {resolve(pathSimplifierIns);})
  })
}
export function addPath(params,pathSimplifierIns,name){
  pathSimplifierIns.clearPathNavigators();
  pathSimplifierIns.render();
  let path = [];
  for(let i = 0 ; i < params.length; i++){
    let position = new AMap.LngLat(params[i].locationLng,params[i].locationLat);
    path.push(position);
  }
  pathSimplifierIns.setData([{
    name: name,
    path: path
  }]);
}

//巡航器开始，暂停
export function navgStatus(status){

}

//初始化地图
export function initMap(map){
  return  mapLoader.then((AMap) =>{
    if(map == undefined){
      map = new AMap.Map('container', {
        viewMode: '2D', // 默认使用 2D 模式，如果希望使用带有俯仰角的 3D 模式，请设置 viewMode: '3D',
        zoom:11, //初始化地图层级
        center: [116.397428, 39.90923] //初始化地图中心点

      });
    }
      return new Promise(resolve => {
        resolve(map);
      })
  });
}

//添加标记
export function getMark(map,queryParams){
  return initMap(map).then(map => {
    let position = new AMap.LngLat(queryParams.lng , queryParams.lat);
    let marker = new AMap.Marker({
      map: map,
      position: position
    })
    map.add(marker);
    let infoWindow  = new  AMapUI.SimpleInfoWindow({
      closeWhenClickMap: true,
      infoTitle: '<strong><%- title %></strong>',
      infoBody: '<div style="width: 500px; font-size: 14px ">'+
        '<p class="my-desc" style="color: grey"><%- body %></p>' +
        '<p class="my-desc" style="color: grey"><%- imei %></p>' +
        '<p class="my-desc" style="color: grey"><%- address %></p>' +
        '<p class="my-desc" style="color: grey"><%- locationTime %></p>' +
        '<p class="my-desc" style="color: grey"><%- phonenumber %></p>' +

      '</div>',
      //模板数据
      infoTplData: {
        title: '位置信息',
        body: '姓名: '+ queryParams.name,
        imei: 'imei: '+ queryParams.termImei,
        address: '地点: '+ queryParams.locationName,
        locationTime: '上传时间: '+ queryParams.locationTime,
        phonenumber: '手机号: '+ queryParams.phonenumber,
      },

    })

    function openInfoWin() {
      infoWindow.open(map, marker.getPosition());
    }
    marker.on('mouseover',function() {
      openInfoWin();
    });
    marker.on('mouseout',function() {
      //openInfoWin();
      infoWindow.close()
    });
    map.setFitView();
    return marker;

  })

}
export function removeMarker(marker,map){
    marker.setMap(null);
    map.setFitView();
}
export function  test(){
  return new Promise(resolve => {
      resolve(100);
  })
}
